package com.flong;


import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.http.HttpStatus;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import com.flong.utils.thrift.support.ThriftApplicationListener;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableAutoConfiguration
@ServletComponentScan
@SpringBootApplication
@EnableAspectJAutoProxy
@EnableWebMvc
@EnableSwagger2 // 启用Swagger
@ComponentScan({ "com.flong.*" })
public class Application extends WebMvcConfigurerAdapter implements CommandLineRunner {

	/**
	 * @Description TODO
	 * http://blog.csdn.net/u012373815/article/details/53240946
	 * https://www.cnblogs.com/ityouknow/p/5891443.html
	 * 
	 * @Author		liangjl
	 * @Date		2018年2月23日 上午11:53:08
	 * @param args
	 * @throws Exception 参数
	 * @return void 返回类型 
	 * @throws
	 */
	public static void main(String[] args) throws Exception {
		// SpringApplication.run(Application.class, args);

		// SpringApplication.run(Application.class, args).addApplicationListener(new
		// ThriftApplicationListener());

		SpringApplicationBuilder listeners = new SpringApplicationBuilder().listeners(new ThriftApplicationListener());
		/*ConfigurableApplicationContext context =*/ listeners.sources(Application.class).run(args);

	}

	@Override
	public void run(String... args) throws Exception {
		System.out.println("服务启动完成!");
	}

	public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.flong";
	public static final String VERSION = "1.0.0";

	
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {
		// super.addResourceHandlers(registry);
		registry.addResourceHandler("/WEB-INF/views/**").addResourceLocations("/WEB-INF/views/");
		/** 添加swagger-ui的资源文件拦截器的支持 */
		registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
		registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
	}

	
	@Bean
	public InternalResourceViewResolver jspViewResolver() {
		InternalResourceViewResolver resolver = new InternalResourceViewResolver();
		resolver.setPrefix("/WEB-INF/views/");
		resolver.setSuffix(".jsp");
		return resolver;
	}

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))// api接口包扫描路径
				//.paths(regex("/restApi.*"))//只对restApi路径下面的所有连接请求
				.paths(PathSelectors.any())// 可以根据url路径设置哪些请求加入文档，忽略哪些请求
				.build();
	}
	
	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("Swagger2 接口文档示例")// 设置文档的标题
				.description("更多内容请关注：https://github.com/jilongliang")// 设置文档的描述->1.Overview
				.version(VERSION)// 设置文档的版本信息-> 1.1 Version information
				.contact("liangjl")
				.termsOfServiceUrl("www.apache.org")// 设置文档的License信息->1.3 License information
				.build();
	}

	@Bean
	public EmbeddedServletContainerCustomizer containerCustomizer() {
		return new EmbeddedServletContainerCustomizer() {
			@Override
			public void customize(ConfigurableEmbeddedServletContainer container) {

				ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
				ErrorPage error403Page = new ErrorPage(HttpStatus.FORBIDDEN, "/403.html");
				ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
				ErrorPage error405Page = new ErrorPage(HttpStatus.METHOD_NOT_ALLOWED, "/405.html");
				ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

				container.addErrorPages(error401Page, error403Page, error404Page, error405Page, error500Page);
			}
		};
	}

	 
}